<?php
/**
 * Created by PhpStorm.
 * User: l
 * Date: 2016/7/21
 * Time: 14:21
 */
namespace app\models;

use Yii;
use yii\db\ActiveRecord;

class Orderform extends ActiveRecord {

    public function getOrders()
    {
        $id = Yii::$app->user->id;
        $orders = Orderform::find()->where(['user_id' => $id,'del_flag'=>'1'])->orderBy('create_time DESC')->asArray()->all();
        return $orders;
    }

    public function getOrdersByStatus($status)
    {
        $id = Yii::$app->user->id;
        $orders = Orderform::find()->where(['user_id' => $id,'del_flag'=>'1' , 'status'=>$status])->orderBy('create_time DESC')->asArray()->all();
        return $orders;
    }


    //后台获取订单列表数据
    public function getTableData($status,$limit,$offset,$search){

        //售后处理的状态合集
        if($status==-2){
            $status = [2,3,5,6];

        }
        $total = (new \yii\db\Query())
            ->from('hcps_orderform')
            ->where(['del_flag' => 1,'status' =>$status])
            ->count();

        $query=new \yii\db\Query();
        if (!empty($search)) {
            $rows = $query->where(['del_flag' => 1,'status'=>$status])->andWhere(['or',['like', 'orderform_id', $search],['like', 'user_name', $search]])->from('hcps_orderform')->orderBy('create_time DESC')->limit($limit)->offset($offset)->all();
        }else{
            $rows = $query->where(['del_flag' => 1,'status'=>$status])->from('hcps_orderform')->orderBy('create_time DESC')->limit($limit)->offset($offset)->all();
        }

        for($i=0;$i<count($rows);$i++){
            $p_arr= (new \yii\db\Query())->from('hcps_orderform_commodity')->where(['orderform_id' => $rows[$i]['orderform_id']])->all();

            $commodity = (new \yii\db\Query())->from('hcps_commodity')->where(['commodity_id' => $p_arr[0]['commodity_id']])->one();

            $rows[$i]['commodity_pic_url']= $commodity['pic_url'];

            $rows[$i]['commodity_name']= $commodity['commodity_name'];

            $rows[$i]['commodity_count']= count($p_arr);//该订单总共有几类商品

            $addressbook= (new \yii\db\Query())->from('hcps_addressbook')->where(['addressbook_id' => $rows[$i]['addressbook_id']])->one();

            $rows[$i]['addressbook']= $addressbook;

        }

        $rs=['total'=>$total,'rows'=>$rows];
        return $rs;

    }

    public function getAllTableData($status){



        if($status==-1){
            $rows = (new \yii\db\Query())
                ->from('hcps_orderform')
                ->where(['del_flag' => 1])
                ->all();
         }else{
            $rows = (new \yii\db\Query())
                ->from('hcps_orderform')
                ->where(['del_flag' => 1,'status'=>$status])
                ->all();
        }


        for($i=0;$i<count($rows);$i++){
            $p_arr= (new \yii\db\Query())->from('hcps_orderform_commodity')->where(['orderform_id' => $rows[$i]['orderform_id']])->all();
            $commodity = (new \yii\db\Query())->from('hcps_commodity')->where(['commodity_id' => $p_arr[0]['commodity_id']])->one();

            $rows[$i]['commodity_pic_url']= $commodity['pic_url'];

            $rows[$i]['commodity_name']= $commodity['commodity_name'];

            $rows[$i]['commodity_count']= count($p_arr);//该订单总共有几类商品

            $addressbook= (new \yii\db\Query())->from('hcps_addressbook')->where(['addressbook_id' => $rows[$i]['addressbook_id']])->one();

            $rows[$i]['addressbook']= $addressbook;

            if($rows[$i]['status']==0){
                $rows[$i]['status_name']='待付款';
            }else if($rows[$i]['status']==1){
                $rows[$i]['status_name']='待发货';
            }else if($rows[$i]['status']==2){
                $rows[$i]['status_name']='退款中';
            }else if($rows[$i]['status']==3){
                $rows[$i]['status_name']='已退款';
            }else if($rows[$i]['status']==4){
                $rows[$i]['status_name']='待收货';
            }else if($rows[$i]['status']==5){
                $rows[$i]['status_name']='退货中';
            }else if($rows[$i]['status']==6){
                $rows[$i]['status_name']='已退货';
            }else if($rows[$i]['status']==7){
                $rows[$i]['status_name']='已完成';
            }else if($rows[$i]['status']==8){
                $rows[$i]['status_name']='已关闭';
            }

        }

        return $rows;

    }


}
